﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 坐标系
    /// </summary>
    /// <remarks>对 SketchUpAPI 中的 SUAxesRef 的包装</remarks>
    public class Axes : Entity
    {
        #region API 函数
        /// <summary>
        /// 创建一个坐标轴
        /// </summary>
        /// <param name="axes"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUAxesCreate(out IntPtr axes);

        /// <summary>
        /// 创建自定义坐标轴
        /// </summary>
        /// <param name="axes"></param>
        /// <param name="origin">原点</param>
        /// <param name="xaxis">x</param>
        /// <param name="yaxis">y</param>
        /// <param name="zaxis">z</param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUAxesCreateCustom(out IntPtr axes, Point3D origin, Vector3D xaxis, Vector3D yaxis, Vector3D zaxis);

        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="axes"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUAxesRelease(ref IntPtr axes);

        /// <summary>
        /// 获取原点
        /// </summary>
        /// <param name="axes"></param>
        /// <param name="origin"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUAxesGetOrigin(IntPtr axes, ref Point3D origin);

        /// <summary>
        /// 设置原点
        /// </summary>
        /// <param name="axes"></param>
        /// <param name="origin"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUAxesSetOrigin(IntPtr axes, Point3D origin);

        /// <summary>
        /// 获取X轴
        /// </summary>
        /// <param name="axes"></param>
        /// <param name="axis"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUAxesGetXAxis(IntPtr axes, ref Vector3D axis);

        /// <summary>
        /// 获取 Y 轴
        /// </summary>
        /// <param name="axes"></param>
        /// <param name="axis"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUAxesGetYAxis(IntPtr axes, ref Vector3D axis);

        /// <summary>
        /// 获取 Z 轴
        /// </summary>
        /// <param name="axes"></param>
        /// <param name="axis"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUAxesGetZAxis(IntPtr axes, ref Vector3D axis);

        /// <summary>
        /// 设置坐标轴
        /// </summary>
        /// <param name="axes"></param>
        /// <param name="xaxis"></param>
        /// <param name="yaxis"></param>
        /// <param name="zaxis"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUAxesSetAxesVecs(IntPtr axes, Vector3D xaxis, Vector3D yaxis, Vector3D zaxis);

        /// <summary>
        /// 获取变换
        /// </summary>
        /// <param name="axes"></param>
        /// <param name="transform"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUAxesGetTransform(IntPtr axes, ref Transformation transform);

        /// <summary>
        /// 获取平面
        /// </summary>
        /// <param name="axes"></param>
        /// <param name="plane"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUAxesGetPlane(IntPtr axes, ref Plane3D plane);
        #endregion

        /// <summary>
        /// 原点
        /// </summary>
        public Point3D Origin
        {
            get
            {
                Point3D p = new Point3D();
                SketchUpExceptionHelper.ThrowSUResult(SUAxesGetOrigin(Id, ref p));
                return p;
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUAxesSetOrigin(Id, value));
            }
        }

        /// <summary>
        /// X 轴
        /// </summary>
        public Vector3D XAxis
        {
            get
            {
                Vector3D v = new Vector3D();
                SketchUpExceptionHelper.ThrowSUResult(SUAxesGetXAxis(Id, ref v));
                return v;
            }
        }

        /// <summary>
        /// Y 轴
        /// </summary>
        public Vector3D YAxis
        {
            get
            {
                Vector3D v = new Vector3D();
                SketchUpExceptionHelper.ThrowSUResult(SUAxesGetYAxis(Id, ref v));
                return v;
            }
        }

        /// <summary>
        /// Z 轴
        /// </summary>
        public Vector3D ZAxis
        {
            get
            {
                Vector3D v = new Vector3D();
                SketchUpExceptionHelper.ThrowSUResult(SUAxesGetZAxis(Id, ref v));
                return v;
            }
        }

        /// <summary>
        /// 获取变换
        /// </summary>
        public Transformation Transform
        {
            get
            {
                Transformation t = new Transformation();
                SketchUpExceptionHelper.ThrowSUResult(SUAxesGetTransform(Id, ref t));
                return t;
            }
        }

        /// <summary>
        /// 平面
        /// </summary>
        public Plane3D Plane
        {
            get
            {
                Plane3D p = new Plane3D();
                SketchUpExceptionHelper.ThrowSUResult(SUAxesGetPlane(Id, ref p));
                return p;
            }
        }

        internal Axes()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUAxesCreate(out Id));
        }

        /// <summary>
        /// 创建自定义坐标轴
        /// </summary>
        /// <param name="origin"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="z"></param>
        /// <returns></returns>
        public static Axes Create(Point3D origin, Vector3D x, Vector3D y, Vector3D z)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUAxesCreateCustom(out IntPtr id, origin, x, y, z));
            return new Axes(id);
        }

        internal Axes(IntPtr id) : base(id) { }

        /// <summary>
        /// 设置坐标轴
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="z"></param>
        public void SetAxis(Vector3D x, Vector3D y, Vector3D z)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUAxesSetAxesVecs(Id, x, y, z));
        }

        public override void Dispose()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUAxesRelease(ref Id));
        }
    }
}